package the.workshop.milktea.interfaces.api;

import io.swagger.annotations.Api;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wisteria.framework.web.APIResult;
import the.workshop.milktea.interfaces.resp.CategoryPageResp;
import the.workshop.milktea.service.CategoryService;
import the.workshop.milktea.service.dto.CategoryPageResultDTO;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "Tea")
@RestController
@RequestMapping("/api/tea/category")
public class ApiCategoryController {

    @Resource
    private CategoryService categoryService;

    @PostMapping("/list")
    public APIResult<List<CategoryPageResp>> list() {
        List<CategoryPageResultDTO> list = categoryService.getList();
        return APIResult.success(CollectionUtils.isEmpty(list) ? null : list.stream().map(this::convertOf).collect(Collectors.toList()));
    }

    private CategoryPageResp convertOf(CategoryPageResultDTO source) {
        CategoryPageResp target = new CategoryPageResp();
        target.setCategoryId(source.getCategoryId());
        target.setName(source.getName());
        return target;
    }
}
